<?php

namespace app\weixin\controller;
use robot\Robot;
use think\Controller;
use think\Db;

class Index extends Controller
{

	public function get_token()
	{

		if(cache('token')){
			return cache('token');
		}else{

			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbac82045a637c02e&secret=7c320fea54e77a01fcc59ff7060b04e2";

			$data = file_get_contents($url);

			$data = json_decode($data,true);

			return cache('token',$data['access_token'],7200);
		}
	}

	public function get_menu()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->get_token();

		$data = file_get_contents($url);


		$data = json_decode($data,true);

		var_dump($data['menu']['button']);
	}

	public function del_menu()
	{
		
		$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->get_token();

		$data = file_get_contents($url);


		$data = json_decode($data,true);

		var_dump($data);
	}

	public function create_menu()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->get_token();

		$data = [
			'button'=>[
				[
					'type'=>'view',
					'name'=>'百度一下',
					'url'=>'http://www.baidu.com',
				],
				[
					'type'=>'view',
					'name'=>'我的社区',
					'url'=>'http://1o533n8683.imwork.net/',
				],
				[
					'name'=>'个人中心',
					'sub_button'=>[
						[
							'type'=>'view',
							'name'=>'修改密码',
							'url'=>'http://1o533n8683.imwork.net/',
						],
						[
							'type'=>'view',
							'name'=>'客服咨询',
							'url'=>'http://www.alimama.com',
						],
						[
							'type'=>'view',
							'name'=>'个人主页',
							'url'=>'http://www.alimama.com',
						]
					]
				],
			]
		];

		$this->web_post($url,$data);
		
	}

	public function send()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$this->get_token();

		$data = [
			'touser'=>$this->get_user(),
			'msgtype'=>'text',
			'text'=>[
				'content'=>'这个是一个群发的消息'
			]
		];

		$this->web_post($url,$data);


	}

	public function moban()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->get_token();

		$url = "http://1o533n8683.imwork.net/message/template/send?access_token=".$this->get_token();

		$data = [
			'touser'=>'oF5Z2v6LqZJQns3MrZTBqm2h9qcw',
			'template_id'=>'PitXU9b-s1-43AobSu83bRnxHVM3gdTB9PmRZE8cDEM',
			'url'=>'http://www.alimama.com',
			'data'=>[
				'num'=>['value'=>'23423423423','color'=>'#E4211E'],
				'end'=>['value'=>'广州番禺中心医院','color'=>'#E4211E'],
				'on'=>['value'=>'广州天河中转站出发','color'=>'#E4211E']
			]
		];

		$this->web_post($url,$data);
	}

	public function get_user()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->get_token();

		$data = file_get_contents($url);


		$data = json_decode($data,true);

		return $data['data']['openid'];

	}

	public function web_post($url,$data)
	{

		$data = json_encode($data,JSON_UNESCAPED_UNICODE);

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, $url);

		curl_setopt($curl, CURLOPT_HEADER, 0);

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

		curl_setopt($curl, CURLOPT_POST, 1);

		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($data)
		));

		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

		$data = curl_exec($curl);

		var_dump($data);
	}
}